import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { LoggerService } from '@/common/logger/logger.service';
import { UploadFileInterface } from './interfaces/file.interface';
import { EnvEnum } from '../enums/env.enum';

@Injectable()
export class UploadService {

  constructor(
    private readonly configService: ConfigService,
    private readonly logger: LoggerService,
  ) {
    this.logger.setContext(UploadService.name)
  }

  async uploadSingle(file: UploadFileInterface) {
    this.logger.log(`${this.uploadSingle.name} was called`);
    return await this.getFileURL(file)
  }

  async uploadMultiple(files: UploadFileInterface[]) {
    this.logger.log(`${this.uploadMultiple.name} was called`);
    return await Promise.all(files.map((file) => this.getFileURL(file)));
  }


  async getFileURL(file: UploadFileInterface) {
    const dateFolder = file.destination.split("files")[1].slice(1);
    return {
      url: `${this.configService.get(
        EnvEnum.UPLOAD_ADDRESS,
      )}/static/${dateFolder}/${file.filename}`,
      name: file.originalname,
    }
  }





}
